//
//  UIView+TExtension.m
//  百思不得姐2
//
//  Created by 陶建 on 15/7/22.
//  Copyright (c) 2015年 taoJ. All rights reserved.
//

#import "UIView+TExtension.h"

@implementation UIView (TExtension)

#pragma mark - frame - Setter方法
#pragma mark 位置

/** x */
- (void)setX:(CGFloat)x
{
    CGRect rect = self.frame;
    rect.origin.x = x;
    self.frame = rect;
}

/** y */
- (void)setY:(CGFloat)y
{
    CGRect rect = self.frame;
    rect.origin.y = y;
    self.frame = rect;
}

/** origin */
- (void)setOrigin:(CGPoint)origin
{
    CGRect rect = self.frame;
    rect.origin = origin;
    self.frame = rect;
}

#pragma mark 大小

/** width */
- (void)setWidth:(CGFloat)width
{
    CGRect rect = self.frame;
    rect.size.width = width;
    self.frame = rect;
}

/** height */
- (void)setHeight:(CGFloat)height
{
    CGRect rect = self.frame;
    rect.size.height = height;
    self.frame = rect;
}

/** size */
- (void)setSize:(CGSize)size
{
    CGRect rect = self.frame;
    rect.size = size;
    self.frame = rect;
}

#pragma mark - frame - Getter方法
#pragma mark 位置

/** x */
- (CGFloat)x
{
    return self.frame.origin.x;
}

/** y */
- (CGFloat)y
{
    return self.frame.origin.y;
}

/** origin */
- (CGPoint)origin
{
    return self.frame.origin;
}

#pragma mark 大小

/** width */
- (CGFloat)width
{
    return self.frame.size.width;
}

/** height */
- (CGFloat)height
{
    return self.frame.size.height;
}

/** size */
- (CGSize)size
{
    return self.frame.size;
}

#pragma mark - bounds
#pragma mark - bounds - Setter方法
/** boundsWidth */
- (void)setBoundsWidth:(CGFloat)width
{
    CGRect rect = self.bounds;
    rect.size.width = width;
    self.bounds = rect;
}

/** boundsHeight */
- (void)setBoundsHeight:(CGFloat)height
{
    CGRect rect = self.bounds;
    rect.size.width = height;
    self.bounds = rect;
}

/** boundsSize */
- (void)setBoundsSize:(CGSize)size
{
    CGRect rect = self.bounds;
    rect.size = size;
    self.bounds = rect;
}

#pragma mark - bounds - Getter方法

/** boundsWidth */
- (CGFloat)boundsWidth
{
    return self.bounds.size.width;
}

/** boundsHeight */
- (CGFloat)boundsHeight
{
    return self.bounds.size.height;
}

/** boundsSize */
- (CGSize)boundsSize
{
    return self.bounds.size;
}

#pragma mark - 显示在主窗口
/**
 *  当前窗口是否显示在主窗口上
 */
- (BOOL)isShowingOnKeyWindow
{
    // CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
    // 主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    // 转换参考的坐标系
    CGRect rectFromKeyWindow = [self convertRect:self.frame toView:keyWindow];
    // 当前的窗口与主窗口 是否有重叠
    BOOL intersectRect = CGRectIntersectsRect(rectFromKeyWindow, keyWindow.bounds);
    
    return intersectRect && !self.hidden && self.alpha > 0.01 && self.userInteractionEnabled && self.window == keyWindow;
}

@end
